/* @author Miachael Antemann 
 * updated by Shanmugaesan Shanmugaratnam
 *  @created on 07.12.2008 15:13:56
 */
package de.fhhannover.inform.wari.grp8.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTable;

import de.fhhannover.inform.wari.grp8.spiel.StrategyHandler;

@SuppressWarnings("serial")
public class StrategyGui extends JFrame implements ActionListener {

	/**
	 * @param args
	 */
	private Gui_Controller mgCon;
	private Color mColor;
	private JTable jTab;
	private JButton jBtn;
	private Container con;
	private String grpName_Arr[] = new String[14];
	private String grpID_Arr[] = new String[14];
	// String data[][] = new String[14][14];
	String mKiType;
	boolean isKiA, isKiB;

	StrategyGui(String kiType, Gui_Controller guiCon) {
		// TODO Auto-generated method stub
		this.mKiType = kiType;
		this.mgCon = guiCon;
		con = this.getContentPane();
		con.setLayout(new BorderLayout());
		jBtn = new JButton("Ok");
		con.setBackground(mColor);

		String grp_name, id;
		int i = 0;
		Set<String> set = StrategyHandler.getStrategyHandler()
				.getStrategyNames();
		Set<String> set2 = StrategyHandler.getStrategyHandler()
				.getStrategyIDs();
		
		Iterator<String> itr = set.iterator();
		Iterator<String> itr2 = set2.iterator();
		while (itr.hasNext()) {
			grp_name = itr.next();
			id = itr2.next();
			grpID_Arr[i] = id;
			grpName_Arr[i] = StrategyHandler.getStrategyHandler()
					.getIDandNameMap().get(id);
			// System.out.println(grp_name + ": " +
			// StrategyHandler.getStrategyHandler().getNamesMap().get(grp_name));
			// System.out.println(id + ": " +
			// StrategyHandler.getStrategyHandler().getIDandNameMap().get(id));
			// System.out.println(str);
			i++;
		}
		String[] columnNames = { "Index", "Gruppen ID", "Ki Name" };
		Object[][] data = new Object[set.size()][3];
		for (int a = 0; a < i; a++) {
			// System.out.println(""+grpID_Arr[a]+" | "+grpName_Arr[a]);
			data[a][0] = Integer.toString(a + 1);
			data[a][1] = grpID_Arr[a];
			data[a][2] = grpName_Arr[a];
		}
		jTab = new JTable(data, columnNames);
		// JScrollPane pane = new JScrollPane(jTab);
		// con.add(jTab.getTableHeader(),BorderLayout.PAGE_START);
		// con.add(jTab,BorderLayout.CENTER);
		con.add(jTab.getTableHeader(), BorderLayout.PAGE_START);
		con.add(jTab, BorderLayout.CENTER);
		if (mKiType.equalsIgnoreCase("A")) {
			setTitle("Ki Chooser A");
		}
		if (mKiType.equalsIgnoreCase("B")) {
			setTitle("Ki Chooser B");
		}
		jBtn.addActionListener(this);
		con.add(jBtn, BorderLayout.SOUTH);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setSize(800, 300);
		this.setLocation(280, 10);
	}

	public void actionPerformed(ActionEvent arg) {
		String command = arg.getActionCommand();
		if (command.equals("Ok")) {
			int rowTab = jTab.getSelectedRow();
			// int col = jTab.getSelectedColumn();
			// System.out.println(""+jTab.getValueAt(row,col));
			if (rowTab != -1) {
				if (mKiType.equalsIgnoreCase("A")) {
					System.out.println("geklickte Zeile A : "
							+ grpID_Arr[rowTab] + " " + grpName_Arr[rowTab]);
					mgCon.setKIPreferences(true, grpName_Arr[rowTab]);
				} else if (mKiType.equalsIgnoreCase("B")) {
					System.out.println("geklickte Zeile B : "
							+ grpID_Arr[rowTab] + " " + grpName_Arr[rowTab]);
					mgCon.setKIPreferences(false, grpName_Arr[rowTab]);
				}
				this.dispose();
			}
		}
	}

	// public static void main(String args[]){
	// StrategyGui stguiA = new StrategyGui("B",null);
	// stguiA.setVisible(true);
	// }
}
